package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.controller.ZnxController;
import com.example.entity.hz.Znx;
import com.example.entity.yifei.Invtl;
import com.example.entity.yifei.Invtm;
import com.example.entity.yifei.Moctm;
import com.example.entity.yifei.Moctn;
import com.example.exception.BaseException;
import com.example.exception.CustomException;
import com.example.mapper.InvtmMapper;
import com.example.service.IActFlowCustomService;
import com.example.service.InvtlService;
import com.example.mapper.InvtlMapper;
import com.example.untils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @author 14501
* @description 针对表【INVTL】的数据库操作Service实现
* @createDate 2024-08-06 08:07:05
*/
@Service
public class InvtlServiceImpl extends ServiceImpl<InvtlMapper, Invtl> implements InvtlService, IActFlowCustomService {

    @Resource
    private InvtlMapper invtlMapper;

    @Resource
    private InvtmMapper invtmMapper;

    @Override
    public Boolean tc30IsN(String tc001, String tc002) {
        LambdaQueryWrapper<Invtl> queryWrapper= new LambdaQueryWrapper<Invtl>();
        queryWrapper.eq(Invtl::getTL001,tc001).eq(Invtl::getTL002,tc002);
        Invtl invtl = invtlMapper.selectOne(queryWrapper);
        if (invtl!=null&&invtl.getTL017().equals("0")){
            return true;
        }else {
            throw new BaseException(tc001+"-"+tc002+"该报废单不是未审核报废单");
        }
    }

    @Override
    public Map<String, Object> setvariables(String id, String userName) {
        Map variables=new HashMap<>();
        variables.put("assignee0",userName);
        variables.put("processInsName","报废单流程");
        return variables;
    }

    @Override
    public Map<String, Object> setvariables(String id, String userName, Object vo) {
        return null;
    }

    @Override
    public void startRunTask(String id) {

    }
    @Autowired
    private ZnxController znxController;

    @Override
    public void endRunTask(String id, Boolean state, String defineId, String initiator) {
        System.out.printf("报废单审批流程结束业务操作！！！！id="+id+"审核状态为："+state);
        String[] strings = id.split(":");
        Invtl invtl = invtlMapper.selectOne(new LambdaQueryWrapper<Invtl>().eq(Invtl::getTL001, strings[0]).eq(Invtl::getTL002, strings[1]));
        if(invtl == null) {
            throw new CustomException("500", "委外核价单不存在");
        }
        if(state) {  // 审核通过
            invtl.setTL017("3");
            invtlMapper.update(invtl, new LambdaQueryWrapper<Invtl>().eq(Invtl::getTL001, strings[0]).eq(Invtl::getTL002, strings[1]));
            checkinvtl(strings[0], strings[1]);
            List<Znx> znxList=new ArrayList<>();
            Znx znx=Znx.builder().code(id).content("有一张新的请购单通过了审核!").departmentCode("90802").notificationCode(initiator).customField(defineId)
                    .title("通知").build();
            znxList.add(znx);
            znxController.insert(znxList);
        }else{  // 审批失败
            invtl.setTL017("2");
            invtlMapper.update(invtl,new LambdaQueryWrapper<Invtl>().eq(Invtl::getTL001, strings[0]).eq(Invtl::getTL002, strings[1]));
        }
    }

    public void checkinvtl(String TA001, String TA002) {
        if(StringUtils.isEmpty(TA001) || StringUtils.isEmpty(TA002)) {
            throw new CustomException("500", "单别或单号为空");
        }
        Invtl invtl = invtlMapper.selectOne(new LambdaQueryWrapper<Invtl>().eq(Invtl::getTL001, TA001).eq(Invtl::getTL002, TA002));
        if(invtl == null) {
            throw new CustomException("500", "单据不存在!");
        }
        invtl.setTL014("Y");
        invtlMapper.update(invtl, new LambdaQueryWrapper<Invtl>().eq(Invtl::getTL001,TA001).eq(Invtl::getTL002,TA002));
        Invtm invtm = new Invtm();
        invtm.setTM021("Y");
        invtmMapper.update(invtm, new LambdaQueryWrapper<Invtm>().eq(Invtm::getTM001, TA001).eq(Invtm::getTM002, TA002));
    }

    @Override
    public Object taskInfo(Map<String, Object> processVariables) {
        return processVariables.get("object");
    }
}




